package com.firefly.web.controller.admin.ums;

import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.ums.*;
import com.firefly.dao.vo.ums.AdminListVo;
import com.firefly.dao.vo.ums.AdminNameListVo;
import com.firefly.dao.vo.ums.AdminVo;
import com.firefly.service.command.ums.AdminCommand;
import com.firefly.service.query.ums.AdminQuery;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.firefly.web.controller.admin.ConsoleApiRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;


/**
 * @author 李志刚
 * @since 2020-04-02
 */
@ConsoleApiRestController("/admin")
public class AdminController {
    private final AdminQuery adminQuery;
    private final AdminCommand adminCommand;

    @Autowired
    public AdminController(AdminQuery adminQuery,AdminCommand adminCommand){
        this.adminQuery = adminQuery;
        this.adminCommand = adminCommand;
    }

    @GetMapping("/list")
    public AdminListVo getList(AdminSearchDto request)  throws Exception{
        return adminQuery.getListAndRoleDepartment(request);
    }

    @GetMapping("/list/names")
    public AdminNameListVo getListNames(AdminSearchDto request)  throws Exception{
        return adminQuery.getList(request);
    }

    @GetMapping("/detail")
    public AdminVo getDetail(@Validated(CommonDto.showDetail.class) CommonDto request) {
        return adminQuery.getDetail(request);
    }

    @PostMapping("/create")
    public Boolean postCreate(@Validated AdminCreateDto request) throws Exception {
        return adminCommand.create(request);
    }

    @PostMapping("/update")
    public Boolean postUpdate(@Validated AdminUpdateDto request) {
        return adminCommand.updateadmin(request);
    }

    @PostMapping("/pwd")
    public Boolean postPwd(@Validated AdminPwdDto request){
        return adminCommand.updatepwd(request);
    }

    @PostMapping("/status")
    public Boolean postStatus(@Validated AdminStatusDto request){
        return adminCommand.updatestatus(request);
    }

    @PostMapping("/remove")
    public Boolean postRemove(@Validated(CommonDto.removeDetail.class) CommonDto request) {
        return adminCommand.remove(request);
    }

    @PostMapping("/removemultiple")
    public Boolean postRemoveMultiple(@Validated(CommonDto.removeMultiple.class) CommonDto request) {
        return adminCommand.removeMultiple(request);
    }

    @PostMapping("/selfinfo")
    public Boolean postSelfEdit(@Validated AdminUpdateSelfDto request, HttpServletRequest httpRequest) throws JsonProcessingException {
        return adminCommand.updateSelf(request, Integer.valueOf(httpRequest.getParameter("fireflyUserId")));
    }

    @PostMapping("/selfpassword")
    public Boolean postSelfPwd(@Validated AdminPwdDto request, HttpServletRequest httpRequest) throws Exception {
        return adminCommand.updateSelfPwd(request, Integer.valueOf(httpRequest.getParameter("fireflyUserId")));
    }
}

